package cn.easy.rest.controller;

import cn.easy.api.ActionService;
import cn.easy.api.UserService;
import cn.easy.common.pojo.ActionType;
import cn.easy.common.pojo.EasyResult;
import cn.easy.pojo.Action;
import cn.easy.pojo.User;
import cn.easy.rest.dao.JedisClient;
import cn.easy.rest.util.UserKeyUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 动作控制层
 *
 * @author 谢子豪
 * @time 2017/10/6
 * @description
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/action")
@Controller
public class ActionController {
    @Reference
    private ActionService actionService;
    @Reference
    private UserService userService;

    @Autowired
    private JedisClient jedisClient;

    /**
     * 新增一个动作
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertAction", method = RequestMethod.POST)
    public EasyResult insertAction(@RequestBody Map map, HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        Action action = new Action();
        /*--------------必要参数检查------------*/
        if (map.containsKey("actionName")) {
            action.setActionName(map.get("actionName").toString());
        } else return EasyResult.fail("动作名不能为空");

        if (map.containsKey("actionDesc")) action.setActionDesc(map.get("actionDesc").toString());//推送信息内容
        else return EasyResult.fail("消息内容不能为空");

        if (StringUtils.isEmpty(action.getActionName())) {
            return EasyResult.fail("动作名不能为空");
        }

        if (StringUtils.isEmpty(action.getActionDesc())) {
            return EasyResult.fail("消息内容不能为空");
        }

        if (map.containsKey("actionType")) {
            int type = Integer.parseInt(map.get("actionType").toString());

            boolean flag = true;
            for (ActionType t : ActionType.values()) {
                if (t.getIndex() == type) {
                    action.setActionType(t.getIndex());
                    flag = false;
                    break;
                }
            }
            if (flag) return EasyResult.fail("未知的动作类型");
        } else {
            return EasyResult.fail("动作种类不能为空");
        }
        /*--------------新增操作------------*/
        try {
            actionService.insertAction(action, userKey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 删除操作
     *
     * @param actionId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAction/{actionId}", method = RequestMethod.GET)
    public EasyResult deleteAction(@PathVariable int actionId,HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        try {
            actionService.deleteAction(actionId, userKey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 更新动作
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAction", method = RequestMethod.POST)
    public EasyResult updateAction(@RequestBody Map map, HttpServletRequest request) {
        Action action = new Action();
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        /*--------------必要参数检查------------*/
        if (map.containsKey("actionType")) {
            int type = Integer.parseInt(map.get("actionType").toString());

            boolean flag = true;
            for (ActionType t : ActionType.values()) {
                if (t.getIndex() == type) {
                    action.setActionType(t.getIndex());
                    flag = false;
                    break;
                }
            }
            if (flag) return EasyResult.fail("未知的动作类型");
        } else {
            return EasyResult.fail("动作种类不能为空");
        }

        if (map.containsKey("actionDesc")) action.setActionDesc(map.get("actionDesc").toString());
        else return EasyResult.fail("缺少消息内容");

        if (map.containsKey("actionId")) action.setActionId(Integer.parseInt(map.get("actionId").toString()));
        else return EasyResult.fail("缺少要更新的动作的主键");


        /*--------------更新------------*/
        try {
            actionService.updateAction(action, userKey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 查询用户名下所有的动作
     *
     * @return
     */
    @RequestMapping(value = "/selectUserAction", method = RequestMethod.GET)
    @ResponseBody
    public EasyResult selectUserAction(HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        try {
            List<Action> list = actionService.selectUserAction(userKey);
            return EasyResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getActionInfo/{actionId}", method = RequestMethod.GET)
    public EasyResult getActionInfo(@PathVariable Integer actionId, HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        Action action = actionService.getActionById(actionId);
        if (action == null) {
            return EasyResult.fail("找不到动作");
        }
        try {
            User user = userService.getUserInfoByUserId(userKey, action.getUserId());
            if (userKey.equals(user.getUserKey())) {
                return EasyResult.ok(action);
            } else {
                return EasyResult.fail("动作与所属的用户身份验证失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

}
